package com.generator.purchaseManage.brandManage.controller;

import com.generator.aboutUser.user.pojo.Users;
import com.generator.purchaseManage.brandManage.pojo.CBrand;
import com.generator.purchaseManage.brandManage.service.impl.BrandService;
import com.generator.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @version 1.0
 * @auther wumei
 * @date 2022/10/04
 * 星期六 21:09
 */
@Controller
@RequestMapping("/brandManage")
public class BrandManageController {

    @Autowired
    private BrandService brandService;

    public BrandService getBrandService() {
        return brandService;
    }
    public void setBrandService(BrandService brandService) {
        this.brandService = brandService;
    }

    /**
     * 分页拿出品牌信息
     * @param brandCondition
     * @param pageModel
     * @param model
     * @return
     */
    @RequestMapping("getBrandByCondition")
    public String getBrandByCondition(CBrand brandCondition, PageModel<CBrand> pageModel, Model model){
        PageModel<CBrand> brandPageModel = brandService.queryBrandByCondition(brandCondition, pageModel);
        model.addAttribute("brandList",brandPageModel);
        return "purchase/brand/brandList";
    }


    /**
     * 分页拿出查询的品牌信息
     * @param brandCondition
     * @param pageModel
     * @return
     */
    @ResponseBody
    @RequestMapping("getBrandByCondition2")
    public PageModel<CBrand> getBrandByCondition2(CBrand brandCondition, PageModel<CBrand> pageModel){
        PageModel<CBrand> brandPageModel = brandService.queryBrandByCondition(brandCondition, pageModel);
        for (CBrand b : brandPageModel.getData()) {
            System.out.println(b);
        }
        return brandPageModel;
    }


    /**
     * 添加品牌
     * @param brand
     * @return
     */
    @ResponseBody
    @RequestMapping("/addBrand")
    public boolean addBrand(CBrand brand, HttpSession session){
        Users user = (Users) session.getAttribute("user");  // 获取当前登录用户信息

        if (user != null){
            brand.setCreateId(user.getUId());
        }
        return brandService.addBrand(brand);
    }

    /**
     * 跳转到修改品牌页面
     * @param brand
     * @param model
     * @return
     */
    @RequestMapping("/toUpdateBrandView/{brandName}")
    public String toUpdateBrandView(CBrand brand,Model model,HttpSession session){
        // 根据品牌名查询到品牌信息
        CBrand updateBrand = brandService.queryBrandByName(brand);
        model.addAttribute("updateBrand",updateBrand);
        // 把当前修改的品牌信息放入session供updateBrand方法使用
        session.setAttribute("brand",updateBrand);
        return "purchase/brand/brandUpdate";
    }

    /**
     * 修改品牌信息
     * @param brand
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateBrand")
    public boolean updateBrand(CBrand brand,HttpSession session){
        CBrand beforeBrand = (CBrand) session.getAttribute("brand");
        // 给修改值之后的品牌信息设置ID
        brand.setBrandId(beforeBrand.getBrandId());
        if (brand != null){
            return brandService.updateBrand(brand);
        }
        return false;
    }

    /**
     * 删除品牌信息
     * @param brand
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteBrand")
    public boolean deleteBrand(CBrand brand){
        System.out.println(brand);
        if (brand != null){
            return brandService.deleteBrand(brand);
        }
        return false;
    }

    /**
     * 获取所有品牌信息传给新增商品类型页面
     * @param model
     * @return
     */
    @RequestMapping("/getBrandByConditionToProductPage")
    public String getBrandByConditionToProductPage(Model model){
        List<CBrand> brandList = brandService.queryBrandByCondition();
        model.addAttribute("brandList",brandList);
        return "purchase/productType/productTypeAdd";
    }

    /**
     * 获取所有品牌信息传给商品类型页面
     * @return
     */
    @ResponseBody
    @RequestMapping("/getBrandByConditionToProductPage2")
    public List<CBrand> getBrandByConditionToProductPage2(){
        return brandService.queryBrandByCondition();
    }




}
